//
//  SplashView.m
//  hehenianMobile
//
//  Created by huhmf on 15/8/18.
//  Copyright (c) 2015年 huihu. All rights reserved.
//

#import "SplashView.h"
#import "AppDelegate.h"
#import "ISplash.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "BaoController.h"


@interface SplashView()

@property(nonatomic,strong) UIImageView *adImageView;
@property(nonatomic,strong) NSString *adImageViewLink;

@end

@implementation SplashView

/**
 *  加载广告
 */
-(void)loadSplashView
{
    AppDelegate *myDelegate=[AppDelegate appDelegate];

        [self didCompleteLoginGestureSucc];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeAdImageView];
        });

}
#pragma mark 实现协议 GestureUnlockDelegate
-(void)didCompleteLoginGestureSucc{
    AppDelegate *myDelegate=[AppDelegate appDelegate];
    //myDelegate.window.rootViewController =myDelegate.LeftSlideVC;
    [GetCoreI(ISplash) requestSplashWithSuccBlock:^(NSDictionary *dict) {
       
        if (dict) {
            _adImageViewLink=[dict valueForKey:@"link"];
            NSString *strPic=[dict valueForKey:@"pic"];
            
            //加载广告启动图
            _adImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
            NSURL *imageURL=[[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@",strPic]];
            [_adImageView sd_setImageWithURL:imageURL placeholderImage:nil];
            
            _adImageView.userInteractionEnabled=YES;
            UITapGestureRecognizer *Tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTouch:)];
            [_adImageView addGestureRecognizer:Tap];
            
            [myDelegate.window addSubview:_adImageView];
            [self performSelector:@selector(removeAdImageView) withObject:nil afterDelay:2];
           
            
        }else{
            myDelegate.window.rootViewController =myDelegate.navigationController;
        }
    } failBLock:^{
         myDelegate.window.rootViewController =myDelegate.navigationController;
    }];
}

/**
 *  移除广告图
 */
- (void)removeAdImageView
{
    AppDelegate *myDelegate=[AppDelegate appDelegate];
    myDelegate.window.rootViewController =myDelegate.navigationController;
    
    [UIView animateWithDuration:0.3f animations:^{
        
        self.adImageView.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        
        self.adImageView.alpha = 0.f;
    } completion:^(BOOL finished) {
        
        [self.adImageView removeFromSuperview];
    }];
    
}


-(void)imageTouch:(UITapGestureRecognizer *)sender
{
    if (!_adImageViewLink) {
        return;
    }
    self.adImageView.userInteractionEnabled=NO;
    [self.adImageView removeFromSuperview];
    
    AppDelegate *myDelegate=[AppDelegate appDelegate];
    
    BaoController *baoVC=[[BaoController alloc]initWithUrl:_adImageViewLink title:@"最新活动"];
    [myDelegate.navigationController pushViewController:baoVC animated:YES];
    
}



@end
